const router = require("koa-router")()
const setupModel = require("../models/accountSetup")
router.prefix("/api");

//删除活动
router.post("/deleteSetup", async (ctx) => {
    ctx.status = 200;
    let data = ctx.request.body;
    let { _id } = data;
    let res = await setupModel.findOneAndDelete({ _id })
    if (res) {
        ctx.body = {
            code: 200,
            msg: "删除活动成功"
        }
    } else {
        ctx.body = {
            code: 400,
            msg: "删除活动失败，请重新操作"
        }
    }
})

//修改活动
router.post("/changeSetup", async (ctx) => {
    ctx.status = 200;
    let data = ctx.request.body;
    let { _id, monney, joinNum, intoNum, intoInfo, userfulDate, addDate, endDate } = data
    let res = await setupModel.findOneAndUpdate({ _id }, { $set: { monney, joinNum, intoNum, intoInfo, userfulDate, addDate, endDate } })
    if (res) {
        ctx.body = {
            code: 200,
            msg: "活动列表更新成功"
        }
    } else {
        ctx.body = {
            code: 400,
            msg: "活动列表更新失败"
        }
    }
})
//添加活动
router.post("/addSetup", async (ctx) => {
    ctx.status = 200
    let data = ctx.request.body
    let addDate = Date.now();
    let { monney, joinNum, intoNum, intoInfo, userfulDate, endDate } = data
    let res = await setupModel.create({ monney, joinNum, intoNum, intoInfo, userfulDate, addDate, endDate })
    ctx.body = {
        code: 200,
        msg: "添加活动成功"
    }
})
//获取所有活动
router.get("/getSetup", async (ctx) => {
    ctx.status = 200
    let res = await setupModel.find({});
    ctx.body = {
        code: 200,
        data: res
    }
})
module.exports = router
